package com.dingreading.cloud.soms.util;

import com.dingreading.cloud.common.util.*;
import com.dingreading.cloud.soms.dto.MaxFullNameDto;
import com.dingreading.cloud.soms.entity.Members;
import com.dingreading.cloud.soms.service.MembersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

@Component
public class MemberBeanUtil {

    public static MemberBeanUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    @Resource
    private MembersService membersService;


    /**
     * 门店添加会员
     */
    public static Members newMember(
            Members data, boolean storeManager, String corpUid, String storeUid, Long storeId, String storeName, String fullName,
            String mob, String wxName, Integer sex, Integer grade, String dateOfBirth,
            String customSource, String customSourceRemark, String channelUid, String channelName,
            String refererMemberUid, String refererMemberName, String refererStaffUid, String refererStaffName, String refererStaffNickName, String refererPubPhone, String refererPriPhone,
            String staffUid, String staffName, String nickName, Date time, String schoolName, String houseName, String source
    ) throws Exception {
        if (null == data) {
            data = new Members();
            data.setCorpUid(corpUid);
            data.setStoreId(storeId);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setFromStoreUid(storeUid);
            data.setFromStoreName(storeName);
            data.setMob(mob);
            data.setWxName(wxName);
            data.setReadBooks(0);
            data.setReadDays(0);
            data.setReadWords(0d);
            data.setEnabled(1);
            data.setFaceId("");
            data.setFaceUpStatus(-1);
            data.setFaceImg("");
            // 设置会员uid
            data.setUid(KitUtil.getUuid());
            // 设置会员系统名称
            data.setMemberName(MemberBeanUtil.getMemberName(fullName));
            // 数据来源
            data.setSource(source);
            // 有效状态 0.待确认 -1.无效 1.有效
            data.setValidStatus(1);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            data.setAddNickName(nickName);
            String password = "123456";
            String md5pass = MD5Util.MD5Encode(password, "UTF-8");
            data.setPassword(md5pass);

            data.setCustomSource(customSource);
            data.setCustomSourceRemark(customSourceRemark);
            data.setChannelUid(channelUid);
            data.setChannelName(channelName);
            data.setRefererMemberUid(refererMemberUid);
            data.setRefererMemberName(refererMemberName);
            data.setRefererStaffUid(refererStaffUid);
            data.setRefererStaffFullName(refererStaffName);
            data.setRefererStaffName(refererStaffNickName);
            data.setRefererStaffPubPhone(refererPubPhone);
            data.setRefererStaffPriPhone(refererPriPhone);
            data.setDatabaseType(1);
        } else {
            // 会员姓名不同时重新设置会员系统名称
            if (!data.getFullName().equals(fullName)) {
                data.setMemberName(MemberBeanUtil.getMemberName(fullName));
            }
            // 非店长不能修改手机号码
            if (storeManager) {
                data.setMob(mob);
                data.setWxName(wxName);
            }
            // 超过30天不能修改来源、渠道和市场人员信息
            boolean b = TimeUtil.moreThanDay(data.getAddTime(), 30);
            if (!b) {
                data.setCustomSource(customSource);
                data.setCustomSourceRemark(customSourceRemark);
                data.setChannelUid(channelUid);
                data.setChannelName(channelName);
                data.setRefererMemberUid(refererMemberUid);
                data.setRefererMemberName(refererMemberName);
                data.setRefererStaffUid(refererStaffUid);
                data.setRefererStaffFullName(refererStaffName);
                data.setRefererStaffName(refererStaffNickName);
                data.setRefererStaffPubPhone(refererPubPhone);
                data.setRefererStaffPriPhone(refererPriPhone);
            }
        }

        data.setFullName(fullName);
        data.setGrade(grade);
        data.setSex(null == sex ? 0 : sex);
        data.setSchoolName(schoolName);
        data.setHouseName(houseName);
        if (StringUtils.isNotBlank(dateOfBirth)) {
            data.setDateOfBirth(DateUtil.parseDate(dateOfBirth));
        } else {
            data.setDateOfBirth(null);
        }

        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setLastNickName(nickName);
        data.setFollowTime(time);
        return data;
    }

    /**
     * 市场或小程序添加会员
     */
    public static Members newMemberByMarket(
            Members data, String corpUid, String storeUid, Long storeId, String storeName, String fullName,
            String mob, String wxName, Integer sex, Integer grade, String dateOfBirth,
            String customSource, String customSourceRemark, String channelUid, String channelName,
            String refererMemberUid, String refererMemberName, String refererStaffUid, String refererStaffName, String refererStaffNickName, String refererPubPhone, String refererPriPhone,
            String staffUid, String staffName, String nickName, Date followTime, String schoolName, String houseName, String source, String lastFollowUid
    ) throws Exception {
        if (null == data) {
            data = new Members();
            data.setCorpUid(corpUid);
            data.setReadBooks(0);
            data.setReadDays(0);
            data.setReadWords(0d);
            data.setEnabled(1);
            data.setFaceId("");
            data.setFaceUpStatus(-1);
            data.setFaceImg("");
            // 设置会员uid
            data.setUid(KitUtil.getUuid());
            // 设置会员系统名称
            data.setMemberName(MemberBeanUtil.getMemberName(fullName));
            // 数据来源
            data.setSource(source);
            // 有效状态 0.待确认 -1.无效 1.有效
            data.setValidStatus(0);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            data.setAddNickName(nickName);

            data.setCustomSource(customSource);
            data.setCustomSourceRemark(customSourceRemark);
            data.setChannelUid(channelUid);
            data.setChannelName(channelName);
            data.setRefererMemberUid(refererMemberUid);
            data.setRefererMemberName(refererMemberName);
            data.setRefererStaffUid(refererStaffUid);
            data.setRefererStaffFullName(refererStaffName);
            data.setRefererStaffName(refererStaffNickName);
            data.setRefererStaffPubPhone(refererPubPhone);
            data.setRefererStaffPriPhone(refererPriPhone);
            data.setDatabaseType(1);
            String password = "123456";
            String md5pass = MD5Util.MD5Encode(password, "UTF-8");
            data.setPassword(md5pass);
        } else {
            // 会员姓名不同时重新设置会员系统名称
            if (!data.getFullName().equals(fullName)) {
                data.setMemberName(MemberBeanUtil.getMemberName(fullName));
            }
            // 超过30天不能修改来源、渠道和市场人员信息
            boolean b = TimeUtil.moreThanDay(data.getAddTime(), 30);
            if (!b) {
                data.setCustomSource(customSource);
                data.setCustomSourceRemark(customSourceRemark);
                data.setChannelUid(channelUid);
                data.setChannelName(channelName);
                data.setRefererMemberUid(refererMemberUid);
                data.setRefererMemberName(refererMemberName);
                data.setRefererStaffUid(refererStaffUid);
                data.setRefererStaffFullName(refererStaffName);
                data.setRefererStaffName(refererStaffNickName);
                data.setRefererStaffPubPhone(refererPubPhone);
                data.setRefererStaffPriPhone(refererPriPhone);
            }
        }

        data.setStoreUid(storeUid);
        data.setStoreId(storeId);
        data.setStoreName(storeName);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setFullName(fullName);
        data.setGrade(grade);
        data.setSex(null == sex ? 0 : sex);
        data.setMob(mob);
        data.setWxName(wxName);
        data.setSchoolName(schoolName);
        data.setHouseName(houseName);
        if (StringUtils.isNotBlank(dateOfBirth)) {
            data.setDateOfBirth(DateUtil.parseDate(dateOfBirth));
        } else {
            data.setDateOfBirth(null);
        }

        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setLastNickName(nickName);
        data.setFollowTime(followTime);
        data.setLastFollowUid(lastFollowUid);
        return data;
    }

    /**
     * 导入时创建会员
     */
    public static Members createMemberByImport(
            String corpUid, String storeUid, String storeName, Long storesId,
            String memberUid, String mob, String name, String grade, String staffUid, String staffName
    ) {
        Members data = new Members();
        data.setMob(mob);
        data.setFullName(name);
        int gradeValue = 0;
        // 幼儿园：小:-3，中:-2，大-1；小学：一:1，二:2，三:3，四:4，五:5，六:6；初中：初一:7，初二:8；未知:0
        if (grade.equals("大班"))
            gradeValue = -1;
        else if (grade.equals("中班"))
            gradeValue = -2;
        else if (grade.equals("小班"))
            gradeValue = -3;
        else if (grade.equals("一年级"))
            gradeValue = 1;
        else if (grade.equals("二年级"))
            gradeValue = 2;
        else if (grade.equals("三年级"))
            gradeValue = 3;
        else if (grade.equals("四年级"))
            gradeValue = 4;
        else if (grade.equals("五年级"))
            gradeValue = 5;
        else if (grade.equals("六年级"))
            gradeValue = 6;
        else if (grade.equals("初一"))
            gradeValue = 7;
        else if (grade.equals("初二"))
            gradeValue = 8;
        else
            gradeValue = 0;
        data.setGrade(gradeValue);

        data.setUid(memberUid);
        // 设置会员系统名称
        data.setMemberName(MemberBeanUtil.getMemberName(data.getFullName()));
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreId(storesId);
        data.setStoreName(storeName);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setSex(0);
        data.setReadBooks(0);
        data.setReadDays(0);
        data.setReadWords(0d);
        data.setEnabled(1);
        data.setFaceId("");
        data.setFaceUpStatus(-1);
        data.setFaceImg("");
        data.setSource("store");
        data.setValidStatus(1);
        data.setDatabaseType(1);
        String password = "123456";
        String md5pass = MD5Util.MD5Encode(password, "UTF-8");
        data.setPassword(md5pass);

        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);

        return data;
    }

    /**
     * 设置系统名称，不重复，末尾数字累加，如：李明2，逢4跳过
     *
     * @param fullName 会员集合
     */
    public static String getMemberName(String fullName) {
        MaxFullNameDto maxFullName = util.membersService.maxFullName(fullName);
        if (maxFullName != null) {
            int maxNumber = NepUtil.nullToZero(maxFullName.getMaxNumber());
            if (maxNumber != 0) {
                maxNumber += 1;
            } else {
                maxNumber += 2;
            }
            // 数字最后一位是4则跳过
            maxNumber = TextUtils.dealLastFour(maxNumber);
            fullName = fullName.concat(String.valueOf(maxNumber));
        }
        return fullName;
    }

}